package com.zdy.today.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zdy.today.R;


public class BaseDialog extends Dialog {
    private TextView mTitleTxt;
    private ImageView mTipImage;
    private Button mCancelBtn;
    private Button mSureBtn;
    private Button mSingleBtn;
    public LinearLayout mContentLayout;
    private LinearLayout mBottomLayout;

    public BaseDialog(Context context) {
        super(context, R.style.SelectDialogStyle);
        setContentView(R.layout.dialog_base);
        mTitleTxt = (TextView) findViewById(R.id.txt_dialog_title);
        mTipImage = (ImageView) findViewById(R.id.img_dialog_tip);
        mCancelBtn = (Button) findViewById(R.id.btn_bottom_cancel);
        mSureBtn = (Button) findViewById(R.id.btn_bottom_sure);
        mSingleBtn = (Button) findViewById(R.id.btn_single);
        mContentLayout = (LinearLayout) findViewById(R.id.layout_bottom_content);
        mBottomLayout = (LinearLayout) findViewById(R.id.layout_dialog_bottom);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.gravity = Gravity.CENTER;
        lp.dimAmount = 0.3f;
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = d.widthPixels * 3 / 4;
        lp.windowAnimations = R.style.SelectDialogStyle;
        getWindow().setAttributes(lp);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    }

    public BaseDialog setTitleText(String title) {
        if (!TextUtils.isEmpty(title)) {
            mTitleTxt.setText(title);
        } else {
            hideTitleText();
        }
        return this;
    }

    public BaseDialog setTitleText(int textRes) {
        mTitleTxt.setText(textRes);
        return this;
    }

    public BaseDialog hideTitleText() {
        if (null != mTitleTxt && View.VISIBLE == mTitleTxt.getVisibility()) {
            mTitleTxt.setVisibility(View.GONE);
        }
        return this;
    }

    public BaseDialog showTitleText() {
        if (null != mTitleTxt) {
            mTitleTxt.setVisibility(View.VISIBLE);
        }
        return this;
    }

    public BaseDialog setContentImageRes(int res) {
        mTipImage.setVisibility(View.VISIBLE);
        mTipImage.setImageResource(res);
        return this;
    }

    public BaseDialog setContent(int viewRes) {
        View.inflate(getContext(), viewRes, mContentLayout);
        return this;
    }

    public BaseDialog setContent(View contentView) {
        if (null != contentView)
            mContentLayout.addView(contentView);
        return this;
    }

    public BaseDialog setOnCancelListener(View.OnClickListener listener) {
        mCancelBtn.setOnClickListener(listener);
        return this;
    }

    public BaseDialog setOnSureListener(View.OnClickListener listener) {
        mSureBtn.setOnClickListener(listener);
        return this;
    }

    public BaseDialog setLeftBtnText(String text) {
        mCancelBtn.setText(text);
        return this;
    }

    public BaseDialog setLeftBtnText(int textRes) {
        mCancelBtn.setText(textRes);
        return this;
    }

    public BaseDialog setRightBtnText(String text) {
        mSureBtn.setText(text);
        return this;
    }

    public BaseDialog setRightBtnText(int textRes) {
        mSureBtn.setText(textRes);
        return this;
    }

    public BaseDialog setSingleBtnTextColor(int color) {
        mSingleBtn.setTextColor(color);
        return this;
    }

    public BaseDialog setSingleBtnText(String text) {
        mSingleBtn.setVisibility(View.VISIBLE);
        mSingleBtn.setText(text);
        hideBottomLayout();
        findViewById(R.id.view_layout_line).setVisibility(View.VISIBLE);
        return this;
    }

    public String getSingleBtnText() {
        return mSingleBtn.getText().toString().trim();
    }

    public BaseDialog setOnSingleBtnListener(View.OnClickListener listener) {
        mSingleBtn.setOnClickListener(listener);
        return this;
    }

    public LinearLayout getContentLayout() {
        return mContentLayout;
    }

    /**
     * 隐藏底部的按钮视图
     */
    public BaseDialog hideBottomLayout() {
        findViewById(R.id.view_layout_line).setVisibility(View.GONE);
        findViewById(R.id.view_dialog_spliter).setVisibility(View.GONE);
        mBottomLayout.setVisibility(View.GONE);
        return this;
    }
}
